﻿<template>
  <a-modal
    title="新增盘点规则"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
		<a-form :form="form">
        <a-form-item label="盘点编码" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入盘点编码" v-decorator="['ruleNo', {rules: [{ required: true, message: '请输入盘点编码！' }]}]" />
        </a-form-item>
        <a-form-item label="规则名称" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入规则名称" v-decorator="['ruleName', {rules: [{ required: true, message: '请输入规则名称！' }]}]" />
        </a-form-item>
        <a-form-item label="盘点类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select style="width: 100%" placeholder="请选择盘点类型" v-decorator="['ruleType', {rules: [{ required: true, message: '请选择盘点类型！' }]}]">
            <a-select-option v-for="(item,index) in ruleTypeData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="盘点模式" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select style="width: 100%" placeholder="请选择盘点模式" v-decorator="['ruleMode', {rules: [{ required: true, message: '请选择盘点模式！' }]}]" @change="onChangeRuleMode">
            <a-select-option v-for="(item,index) in ruleModeData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item v-if="ruleMode==='1'" label="计划数" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number style="width: 100%" :min="0.000001" :max="100" :formatter="value => value?`${value}%`:''" :parser="value => value.replace('%', '')"
            v-decorator="['ruleModeValue', {rules: [{ required: true, message: '请输入计划数！' }]}]" />
        </a-form-item>
        <a-form-item v-if="ruleMode==='2'" label="计划数" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number style="width: 100%" :min="0.000001" :max="100" :formatter="value => value?`${value}件`:''" :parser="value => value.replace('件', '')"
            v-decorator="['ruleModeValue', {rules: [{ required: true, message: '请输入计划数！' }]}]" />
        </a-form-item>
        <a-form-item label="盘点周期" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select style="width: 100%" placeholder="请选择盘点周期" v-decorator="['ruleCycle', {rules: [{ required: true, message: '请选择盘点周期！' }]}]">
            <a-select-option v-for="(item,index) in ruleCycleData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import {
    InventoryRuleAdd
  } from '@/api/modular/main/InventoryRuleManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        ruleTypeData: [],
        ruleModeData: [],
        ruleCycleData: [],
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this),
        ruleMode:null
      }
    },
    created(){
      const ruleTypeOption = this.$options
      this.ruleTypeData = ruleTypeOption.filters['dictData']('RuleType')
      const ruleModeOption = this.$options
      this.ruleModeData = ruleModeOption.filters['dictData']('RuleMode')
      const ruleCycleOption = this.$options
      this.ruleCycleData = ruleCycleOption.filters['dictData']('RuleCycle')
    },
    methods: {
      // 初始化方法
      add (record) {
        this.visible = true
        this.$nextTick(() => {

        });
        
      },
      onChangeRuleMode(val){
        this.ruleMode = val
      },
      /**
       * 提交表单
       */
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object') {
                values[key] = JSON.stringify(values[key])
              }
            }
            InventoryRuleAdd(values).then((res) => {
              if (res.success) {
                this.$message.success('新增成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('新增失败：' + JSON.stringify(res.message))
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.ruleMode = null
        this.visible = false
      }
    }
  }
</script>
